import Vue from 'vue'
import VueRouter from 'vue-router'
import Layout from '@/layout'
import store from '@/store'

Vue.use(VueRouter)

// 路由配置
const routes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },
  {
    path: '/register',
    component: () => import('@/views/register/index'),
    hidden: true
  },
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    meta: { requiresAuth: true },
    children: [
      {
        path: 'dashboard',
        name: 'Dashboard',
        component: () => import('@/views/dashboard/index'),
        meta: { title: '首页', icon: 'dashboard' }
      }
    ]
  },
  {
    path: '/message',
    component: Layout,
    meta: { title: '消息管理', icon: 'message', requiresAuth: true },
    children: [
      {
        path: '',
        name: 'Message',
        component: () => import('@/views/message/message'),
        meta: { title: '消息列表' }
      }
    ]
  },
  {
    path: '/order',
    component: Layout,
    meta: { title: '订单管理', icon: 'order', requiresAuth: true },
    children: [
      {
        path: '',
        name: 'Order',
        component: () => import('@/views/order/list'),
        meta: { title: '订单管理' }
      }
    ]
  },
  {
    path: '/user',
    component: Layout,
    meta: { title: '用户管理', icon: 'user', requiresAuth: true },
    children: [
      {
        path: '',
        name: 'User',
        component: () => import('@/views/user/list'),
        meta: { title: '用户管理' }
      }
    ]
  },
  {
    path: '/goods',
    component: Layout,
    redirect: '/goods/list',
    meta: { title: '商品管理', icon: 'goods', requiresAuth: true },
    children: [
      {
        path: 'list',
        name: 'GoodsList',
        component: () => import('@/views/goods/list'),
        meta: { title: '商品列表' }
      },
      {
        path: 'category',
        name: 'GoodsCategory',
        component: () => import('@/views/goods/category'),
        meta: { title: '商品分类' }
      }
    ]
  },
  {
    path: '/log',
    component: Layout,
    redirect: '/log/list',
    meta: { title: '日志管理', icon: 'documentation', requiresAuth: true },
    children: [
      {
        path: 'list',
        name: 'LogList',
        component: () => import('@/views/log/LogView'),
        meta: { title: '系统日志' }
      }
    ]
  },
  {
    path: '/interaction',
    component: Layout,
    redirect: '/interaction/list',
    meta: { title: '互动管理', icon: 'message', requiresAuth: true },
    children: [
      {
        path: 'list',
        name: 'InteractionList',
        component: () => import('@/views/interaction/list'),
        meta: { title: '点赞收藏' }
      }
    ]
  },
  {
    path: '/comment',
    component: Layout,
    redirect: '/comment/list',
    meta: { title: '评论管理', icon: 'edit', requiresAuth: true },
    children: [
      {
        path: 'list',
        name: 'CommentList',
        component: () => import('@/views/comment/list'),
        meta: { title: '评论列表' }
      }
    ]
  },
  // 404 页面
  {
    path: '/404',
    component: () => import('@/views/error-page/404'),
    hidden: true
  },
  {
    path: '*',
    redirect: '/404',
    hidden: true
  }
]

const router = new VueRouter({
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  // 检查是否尝试重复导航到同一地址
  if (to.path === from.path && to.query === from.query && to.hash === from.hash) {
    console.warn('尝试重复导航到同一地址')
    return
  }

  // 检查用户是否有权访问目标路由
  const hasAccess = checkAccess(to)

  if (hasAccess) {
    // 用户有权访问，继续执行路由
    next()
  } else {
    // 用户无权访问，重定向到登录页面
    next({ 
      path: '/login',
      query: { redirect: to.fullPath }
    })
  }
})

function checkAccess(route) {
  // 检查路由是否需要认证
  if (route.meta.requiresAuth && !isAuthenticated()) {
    return false
  }
  return true
}

function isAuthenticated() {
  // 检查用户是否已登录
  return store.getters['user/isLoggedIn']
}

export default router
